/*
 * log.c
 *
 * Created: 27/04/2012 14:33:46
 *  Author: Dani
 */ 

#include <avr/io.h>
#define F_CPU 8000000
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>


uint16_t LogRead(uint8_t product_number)
{
	// Wait for the EEPROM to be ready
	eeprom_busy_wait();
	return eeprom_read_word((uint16_t*)product_number);	
}

void LogWrite(uint8_t product_number, uint16_t product_weight)
{
	// Disable interrupts before writing on the EEPROM.
	cli();
	// Wait for the EEPROM to be ready before writing
	// Write the high byte.
	eeprom_busy_wait();
	eeprom_write_word((uint16_t*)product_number, product_weight);
		// Enable interrupts
	sei();
}

void LogUpdate(uint8_t product_number, uint8_t weight_substaction)
{
	// Save previous weight.
	uint16_t weight = LogRead(product_number);
	// Check that weight saved is higher than weight subtracted
	if (weight > weight_substaction)
	{		
		LogWrite(product_number,(weight - weight_substaction));
	}
	else
	{
		// ------------------
	}		
}

